package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum SaveErrorCode implements EnumTrans<CompleteEntity.SaveErrorCode, SaveErrorCode> {
    /**
     * 默认属性
     * 保存成功
     * 保存失败
     */
    SAVE_DEFAULT(CompleteEntity.SaveErrorCode.SAVE_DEFAULT),
    SAVE_SUCCESS(CompleteEntity.SaveErrorCode.SAVE_SUCCESS),
    SAVE_FAILURE(CompleteEntity.SaveErrorCode.SAVE_FAILURE),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.SaveErrorCode saveErrorCode;

    SaveErrorCode(CompleteEntity.SaveErrorCode saveErrorCode) {
        this.id = saveErrorCode.getNumber();
        this.name = saveErrorCode.name();
        this.saveErrorCode = saveErrorCode;
    }

    /**
     * @param saveErrorCode
     * @return 对应的类型
     */
    public static SaveErrorCode getSaveErrorCodeStruct(CompleteEntity.SaveErrorCode saveErrorCode) {
        Objects.requireNonNull(saveErrorCode, "saveErrorCode can not be null.");
        return Arrays.stream(SaveErrorCode.values())
                .filter(e -> e.name.equals(saveErrorCode.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.SaveErrorCode toProto() {
        return this.saveErrorCode;
    }

    @Override
    public SaveErrorCode formProto(CompleteEntity.SaveErrorCode saveErrorCode) {
        return getSaveErrorCodeStruct(saveErrorCode);
    }
}

